package com.ruoyi.advertisement.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.advertisement.domain.AdvertisementMaterial;
import com.ruoyi.advertisement.service.IAdvertisementMaterialService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 广告素材Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/material")
public class AdvertisementMaterialController extends BaseController
{
    @Autowired
    private IAdvertisementMaterialService advertisementMaterialService;

    /**
     * 查询广告素材列表
     */
    @RequiresPermissions("advertisement:material:list")
    @GetMapping("/list")
    public TableDataInfo list(AdvertisementMaterial advertisementMaterial)
    {
        startPage();
        List<AdvertisementMaterial> list = advertisementMaterialService.selectAdvertisementMaterialList(advertisementMaterial);
        return getDataTable(list);
    }

    /**
     * 导出广告素材列表
     */
    @RequiresPermissions("advertisement:material:export")
    @Log(title = "广告素材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvertisementMaterial advertisementMaterial)
    {
        List<AdvertisementMaterial> list = advertisementMaterialService.selectAdvertisementMaterialList(advertisementMaterial);
        ExcelUtil<AdvertisementMaterial> util = new ExcelUtil<AdvertisementMaterial>(AdvertisementMaterial.class);
        util.exportExcel(response, list, "广告素材数据");
    }

    /**
     * 获取广告素材详细信息
     */
    @RequiresPermissions("advertisement:material:query")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return success(advertisementMaterialService.selectAdvertisementMaterialByMaterialId(materialId));
    }

    /**
     * 新增广告素材
     */
    @RequiresPermissions("advertisement:material:add")
    @Log(title = "广告素材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvertisementMaterial advertisementMaterial)
    {
        return toAjax(advertisementMaterialService.insertAdvertisementMaterial(advertisementMaterial));
    }

    /**
     * 修改广告素材
     */
    @RequiresPermissions("advertisement:material:edit")
    @Log(title = "广告素材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvertisementMaterial advertisementMaterial)
    {
        return toAjax(advertisementMaterialService.updateAdvertisementMaterial(advertisementMaterial));
    }

    /**
     * 删除广告素材
     */
    @RequiresPermissions("advertisement:material:remove")
    @Log(title = "广告素材", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(advertisementMaterialService.deleteAdvertisementMaterialByMaterialIds(materialIds));
    }

    /**
     * 启用/停用广告素材
     */
    @RequiresPermissions("advertisement:material:edit")
    @Log(title = "广告素材状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult changeStatus(@RequestBody AdvertisementMaterial advertisementMaterial)
    {
        return toAjax(advertisementMaterialService.updateAdvertisementMaterial(advertisementMaterial));
    }

    /**
     * 上传广告素材文件
     */
    @RequiresPermissions("advertisement:material:upload")
    @Log(title = "广告素材上传", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file, 
                            @RequestParam("materialType") Integer materialType)
    {
        try {
            AdvertisementMaterial material = advertisementMaterialService.uploadMaterial(file, materialType);
            return AjaxResult.success(material);
        } catch (Exception e) {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 根据素材类型查询素材列表
     */
    @RequiresPermissions("advertisement:material:query")
    @GetMapping("/type/{materialType}")
    public AjaxResult listByType(@PathVariable("materialType") Integer materialType)
    {
        List<AdvertisementMaterial> list = advertisementMaterialService.selectMaterialsByType(materialType);
        return success(list);
    }

    /**
     * 根据状态查询素材列表
     */
    @RequiresPermissions("advertisement:material:query")
    @GetMapping("/status/{status}")
    public AjaxResult listByStatus(@PathVariable("status") Integer status)
    {
        List<AdvertisementMaterial> list = advertisementMaterialService.selectMaterialsByStatus(status);
        return success(list);
    }

    /**
     * 更新素材播放次数
     */
    @PostMapping("/view/{materialId}")
    public AjaxResult incrementViewCount(@PathVariable("materialId") Long materialId)
    {
        return toAjax(advertisementMaterialService.incrementViewCount(materialId));
    }

    /**
     * 批量查询素材信息
     */
    @RequiresPermissions("advertisement:material:query")
    @PostMapping("/batch")
    public AjaxResult batchSelect(@RequestBody List<Long> materialIds)
    {
        List<AdvertisementMaterial> list = advertisementMaterialService.selectMaterialsByIds(materialIds);
        return success(list);
    }

    /**
     * 获取素材统计信息
     */
    @RequiresPermissions("advertisement:material:query")
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        IAdvertisementMaterialService.MaterialStatistics statistics = 
            advertisementMaterialService.getMaterialStatistics();
        return success(statistics);
    }

    /**
     * 启用素材列表（用于下拉选择）
     */
    @GetMapping("/enabled")
    public AjaxResult getEnabledMaterials()
    {
        List<AdvertisementMaterial> list = advertisementMaterialService.selectMaterialsByStatus(1);
        return success(list);
    }
} 